/*
 * @Author: lihaogn
 * @Date: 2021-12-16 23:07:07
 * @LastEditTime: 2022-05-27 13:58:07
 * @LastEditor: lihaogn
 * @Description: 角色模型
 * @FilePath: \lix-admin-server\app\model\role.js
 */
"use strict";
module.exports = (app) => {
  const { STRING, DATE } = app.Sequelize;

  // app.model 相当于 sequelize 实例
  const Role = app.model.define(
    "role",
    {
      id: { type: STRING(40), primaryKey: true },
      name: {
        type: STRING(20),
        allowNull: false,
        unique: "uk_role_name",
      },
      description: {
        type: STRING(30),
      },
      createdAt: { type: DATE, field: "created_at" },
      updatedAt: { type: "TIMESTAMP", field: "updated_at" },
    },
    {
      // 指定数据库中表的名字
      tableName: "roles",
    }
  );

  Role.associate = function () {
    app.model.Role.belongsToMany(app.model.User, {
      as: "user",
      through: "UserRole",
    });
    app.model.Role.belongsToMany(app.model.Permission, {
      as: "permission",
      through: "RolePermission",
    });
  };

  return Role;
};
